package com.imooc.springboot.consumer;

import com.imooc.rabbitmq.utils.JacksonUtil;
import com.imooc.springboot.entity.Order;
import com.imooc.springboot.entity.StandDataItem;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.messaging.Message;
import org.springframework.messaging.handler.annotation.Headers;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.sql.SQLOutput;
import java.util.Map;

@Component
public class RabbitReceiver {

    /**
     * 使用springboot api 发送、接收字符串消息
     * @param message
     * @param channel
     * @throws IOException
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "queue-1", durable = "true"),
            exchange = @Exchange(value = "exchange-1",durable = "true",
                    type ="topic", ignoreDeclarationExceptions = "true"),
            key = "springboot.#")
    )  //如果队列、交换机、绑定关系不存在会自动创建
    @RabbitHandler
    public void onMessage(Message message, Channel channel) throws IOException {
        System.out.println("recv:"+message.getPayload());
        Long deliveryTag = (Long) message.getHeaders().get(AmqpHeaders.DELIVERY_TAG);
        //手工ACK
        channel.basicAck(deliveryTag,false);
    }

    /**
     * 使用springboot api 生产者发送对象消息，消费者直接接收对象，对象需要序列化。
     * @param order
     * @param channel
     * @param headers
     * @throws Exception
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "${spring.rabbitmq.listener.order.queue.name}",
                    durable = "${spring.rabbitmq.listener.order.queue.durable}"),
            exchange = @Exchange(value = "${spring.rabbitmq.listener.order.exchange.name}",
                    durable = "${spring.rabbitmq.listener.order.exchange.durable}",
                    type ="${spring.rabbitmq.listener.order.exchange.type}",
                    ignoreDeclarationExceptions = "${spring.rabbitmq.listener.order.exchange.ignoreDeclarationExceptions}"),
            key = "${spring.rabbitmq.listener.order.key}")
    )
    @RabbitHandler
    public void onOrderMessage(@Payload com.imooc.springboot.entity.Order order,
                               Channel channel, @Headers Map<String, Object> headers) throws Exception{
        System.out.println("recv_> id:"+order.getId()+" _name:"+order.getName());
        Long deliveryTag = (Long) headers.get(AmqpHeaders.DELIVERY_TAG);
        //手工ACK
        channel.basicAck(deliveryTag,false);
    }


    /**
     * 使用 json 传数据，然后解析，实现用原生rabbitmq发消息，使用springboot api接受消息
     * @param obj
     * @param channel
     * @param headers
     * @throws Exception
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "queue-0225", durable = "true"),
            exchange = @Exchange(value = "exchange-0225",durable = "true",
                    type ="topic", ignoreDeclarationExceptions = "true"),
            key = "obj.#")
    )
    @RabbitHandler
    public void onOrderJsonMessage(@Payload String obj,
                                   Channel channel, @Headers Map<String, Object> headers) throws Exception{
        Order order = JacksonUtil.readValue(obj, Order.class);
        System.out.println("recv:"+obj);
        Long deliveryTag = (Long) headers.get(AmqpHeaders.DELIVERY_TAG);
        //手工ACK
        channel.basicAck(deliveryTag,false);
    }

}
